package com.ty.day5;

import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 *
 * @author : txy
 * @create : 2023/11/10 17:29
 */
public class Task1 {
    public static void main(String[] args) {
        //系数
        int[] coefficient = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        //校验码
        char[] cheak = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
        //sum求和，count计算位数，i系数的下标
        int sum = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入身份证号:");
        String x = sc.next();
        String x1 = x.toUpperCase();
        //转换为char数组
        char[] array = x1.toCharArray();
        //如果位数不足18为则代表身份证不合法
        if (array.length != 18) {
            System.out.println("身份证不合法");
            return;
        }
        
        for (int j = 0; j < 17; j++) {
            if (array[j]<'0'||array[j]>'9'){
                System.out.println("身份证不合法");
                return;
            }
            sum += (array[j]-'0') * coefficient[j];
        }
        //得到余数
        int result = sum % 11;
        //取出校验码对比
        if (cheak[result] == array[array.length-1]) {
            System.out.println("身份证号正确\n");
        } else {
            System.out.println("身份证号错误");
        }
    }
}
